package com.itheima.service;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class StudentServiceTest {
    private SqlSession sqlSession;

    @Before
    public void init()throws Exception{
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //获取工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取连接
      sqlSession = factory.openSession();

    }

    @After
    public void close(){
        //关流，释放资源
        sqlSession.commit();
        sqlSession.close();
    }


    @Test
    public void add(){
       //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        Student stu=new Student();
        stu.setName("一号种子选手");
        stu.setAge(26);
        mapper.add(stu);
    }

    @Test
    public void delete(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        mapper.delete(2);
    }

    @Test
    public void update(){

        Student stu=new Student();
        stu.setId(3);
        stu.setName("二号种子选手");
        stu.setAge(34);
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        mapper.update(stu);
    }

    @Test
    public void findAll(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        List<Student> stus = mapper.findAll();
        for (Student stu : stus) {
            System.out.println(stu);
        }
    }

    /*根据学号查询*/
    @Test
    public void findById(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        Student f6 = mapper.findById(6);
        System.out.println(f6);
    }

    /*使用@Param注解改名，根据学号或者姓名查询*/
    @Test
    public void findByIdOrName(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        List<Student> list = mapper.findByIdOrName(6, "张三");
        for (Student student : list) {
            System.out.println(student);
        }
    }

    /*动态if判断多条件查询*/
    @Test
    public void findCondition(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        Student stu=new Student();
       // stu.setId(2);
        stu.setName("二号种子选手");
       // stu.setAge(23);
        Student stufc = mapper.findCondition(stu);
        System.out.println(stufc);

    }

    /*动态if判断多条件查询2*/
    @Test
    public void findCondition2(){
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        Student stu=new Student();
        // stu.setId(2);
        stu.setName("二号种子选手");
        // stu.setAge(23);
        Student stufc2 = mapper.findCondition2(stu);
        System.out.println(stufc2);
    }

    /*动态foreach根据多个学号进行查询*/
    @Test
    public void findByIds(){
        ArrayList<Integer>stusId=new ArrayList<>();
        stusId.add(1);
        stusId.add(3);
        stusId.add(6);
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        ArrayList<Student> byIds = mapper.findByIds(stusId);
        for (Student id : byIds) {
            System.out.println(id);
        }
    }

    /*动态foreach根据多个学号进行查询2*/
    @Test
    public void findByIds2(){
      //模拟实际开发，页面传递多个参数数组
        int[]arrIds={1,3,6};
        //获取Mapper代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //调用mapper的方法
        ArrayList<Student> byIds2 = mapper.findByIds2(arrIds);
        for (Student id2 : byIds2) {
            System.out.println(id2);
        }
    }




}
